﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Xml;
using System.Data;
using System.Net;
using System.IO;

namespace PHPostClient.Web
{
    public class HelperFunctions
    {
        public string BuildYahooURI(string strTicker, string strStartDate, string strEndDate)
        {
            string strReturn = "";
            DateTime dStart = Convert.ToDateTime(strStartDate);
            DateTime dEnd = Convert.ToDateTime(strEndDate);
            string sStartDay = dStart.Day.ToString();
            string sStartMonth = (dStart.Month -1).ToString();
            string sStartYear = dStart.Year.ToString();
            string sEndDay = dEnd.Day.ToString();
            string sEndMonth = (dEnd.Month - 1).ToString();
            string sEndYear = dEnd.Year.ToString();
            StringBuilder sYahooURI = new StringBuilder("http://ichart.finance.yahoo.com/table.csv?s=");
            sYahooURI.Append(strTicker); sYahooURI.Append("&a=");
            sYahooURI.Append(sStartMonth); sYahooURI.Append("&b=");
            sYahooURI.Append(sStartDay);

            sYahooURI.Append("&c=");
            sYahooURI.Append(sStartYear);
            sYahooURI.Append("&d=");
            sYahooURI.Append(sEndMonth);
            sYahooURI.Append("&e=");
            sYahooURI.Append(sEndDay);
            sYahooURI.Append("&f=");
            sYahooURI.Append(sEndYear);
            sYahooURI.Append("&g=d");
            sYahooURI.Append("&ignore=.csv");
            strReturn = sYahooURI.ToString();
            
            return strReturn;
        }

        public XmlDocument getXML(string strTicker, string strStartDate, string strEndDate)
        {
            XmlDocument xReturn = new XmlDocument();
            DataSet result = new DataSet();
            string sYahooURI = BuildYahooURI(strTicker, strStartDate, strEndDate);
            WebClient wc = new WebClient();
            Stream yData = wc.OpenRead(sYahooURI);
            result = GenerateDataSet(yData);
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmlTextwriter = new XmlTextWriter(stringWriter);
            result.WriteXml(xmlTextwriter, XmlWriteMode.WriteSchema);
            XmlNode xRoot = xReturn.CreateElement("root");
            xReturn.AppendChild(xRoot);
            xReturn.LoadXml(stringWriter.ToString());
            return xReturn;
        }

        public DataSet GenerateDataSet(Stream yData)
        {
            DataSet result = new DataSet();
            StreamReader sRead = new StreamReader(yData);
            string[] columns = sRead.ReadLine().Split(',');
            result.Tables.Add("TimeSeries");
            foreach (string col in columns)
            {
                // Удаляем пробелы из имен 
                string thiscol = col.Replace(" ", ""); 
                // Добавляем имя столбца 
                result.Tables["TimeSeries"].Columns.Add(thiscol);
            } 
            string sData = sRead.ReadToEnd();
            string[] rows = sData.Split('\n');
            foreach (string row in rows)
            {
                string[] items = row.Split(',');
                result.Tables["TimeSeries"].Rows.Add(items);
            }
            
            return result;
        }

    }
}